import os, sys, Numeric, math;

param_count = 29;
filter_param = (0, 1, 4, 7, 16);
filtered_paramcount = param_count-len(filter_param);
node_count = 100;
exp_id = 0;

param_name = ( "msgsent_total", "msgsent_route", "msgsent_conf", "msgsent_split", "bytesent_total", "bytesent_route", "bytesent_forward", 
  "msgsent_forward", "maxusage_inputbuffer", "maxusage_outputbuffer", "msgsent_extern", "msgrcv_total", "msgrcv_route", "msgrcv_forward", 
  "msgrcv_conf", "msgrcv_split", "bytercv_total", "bytercv_route", "bytercv_forward", "bytesent_payload", "bytercv_payload", "bytesent_totalnoconf", 
  "msggroup_sent", "maxusage_group");

method_name = ("Naive", "Base", "Innet", "Innet+cm", "Innet+cmp", "Innet+cmg", "Innet+cmpg");
phase_name = ("init", "comp");
column_name = ("Total", "Max", "Node");


def gen_stats(dir):
	stats = Numeric.zeros((filtered_paramcount ,3), 'f');

        try:
                f = open(dir+"stats_aggr.txt", "r");
                ll = f.readlines();
                for j in range(0, filtered_paramcount):
                        l = ll[j].split();
                        stats[j][0] = long(l[0]);
                        stats[j][1] = long(l[1]);
                        stats[j][2] = long(l[2]);

                f.close();
                return stats;
        except:
                i = 0;

	for i in range(0, node_count):
		f_name = dir+"stats"+str(i)+".txt";
		try:
			f = open(f_name, "r");
		except:
			print("Cannot open "+f_name);
#			sys.exit();
			continue;

		ll = f.readlines();
		if (len(ll) < 1):
			continue;

		l = ll[0].split('\n')[0].split(' ');
		k = 0;
		for j in range(0, param_count):
			if (j in filter_param):
				continue;
			stats[k][0] = stats[k][0] + long(l[j]);
			if (long(l[j]) > stats[k][1]):
				stats[k][1] = long(l[j]);
				stats[k][2] = i;
			k = k+1;

		f.close();

	return stats;


def writeresult_joinmethods(result_filename, sprs, scale, dim_x, dim_y, top, tc, srcsel_config, join_sel):
	print("Writing "+result_filename);
	f_out = open(result_filename, 'w');

	for i in range(0, dim_y):
		if (i % filtered_paramcount == 0):
			f_out.write("\n\n");
			f_out.write(top+"/tc"+tc+"/ss"+str(srcsel_config[i/filtered_paramcount][0])+"-"+str(srcsel_config[i/filtered_paramcount][1])+" ");
			for j in range(0, dim_x/3):
				f_out.write(method_name[j/len(join_sel)/2]+" "+phase_name[j % 2]+" "+join_sel[(j/2) % len(join_sel)]+" ");
			f_out.write("\n");
						
			f_out.write(". ");
			for j in range(0, dim_x):
				f_out.write(column_name[j % 3]+" ");								
			f_out.write("\n");

		f_out.write(param_name[i % filtered_paramcount]+" ");
		for j in range(0, dim_x):
			sprs[i][j] = sprs[i][j]/scale;
			f_out.write(str(int(sprs[i][j]))+" ");
		f_out.write("\n");

	f_out.close();


def writeresult_joinmethods_learn(result_filename, sprs, scale, dim_x, dim_y, top, tc):
	print("Writing "+result_filename);
	f_out = open(result_filename, 'w');

	for i in range(0, dim_y):
		if (i % filtered_paramcount == 0):
                        f_out.write("\n\n");
                        for j in range(0, dim_x/3):
                                f_out.write(" . " +str(j)+" . ");
                        f_out.write("\n");

                        f_out.write(". ");
                        for j in range(0, dim_x):
                                f_out.write(column_name[j % 3]+" ");
                        f_out.write("\n");

                f_out.write(param_name[i % filtered_paramcount]+" ");
		for j in range(0, dim_x):
			sprs[i][j] = sprs[i][j]/scale;
			f_out.write(str(int(sprs[i][j]))+" ");
		f_out.write("\n");

	f_out.close();


def writeresult_joinvar(result_filename, sprs, scale, dim_x, dim_y, top, tc, realsel_config, jpsel_config, join_sel):
	print("Writing "+result_filename);
	f_out = open(result_filename, 'w');

	for i in range(0, dim_y):
		if (i % filtered_paramcount == 0):
			f_out.write("\n\n");
			f_out.write(top+"/tc"+tc+"/real_sel"+str(realsel_config[i/filtered_paramcount][0])+"-"+str(realsel_config[i/filtered_paramcount][1])+" ");
			for j in range(0, dim_x/3):
				f_out.write(" . used"+str(jpsel_config[j][0])+"-"+str(jpsel_config[j][1])+" . ");
			f_out.write("\n");
						
			f_out.write(". ");
			for j in range(0, dim_x):
				f_out.write(column_name[j % 3]+" ");								
			f_out.write("\n");

		f_out.write(param_name[i % filtered_paramcount]+" ");
		for j in range(0, dim_x):
			sprs[i][j] = sprs[i][j]/scale;
			f_out.write(str(int(sprs[i][j]))+" ");
		f_out.write("\n");

	f_out.close();


def writeresult_ghtjoin(result_filename, sprs, scale, dim_x, dim_y, top, tc, srcsel_config, join_sel):
        print("Writing "+result_filename);
        f_out = open(result_filename, 'w');

        for i in range(0, dim_y):
                if (i % filtered_paramcount == 0):
                        f_out.write("\n\n");
                        f_out.write(top+"/tc"+tc+"/ss"+str(srcsel_config[i/filtered_paramcount][0])+"-"+str(srcsel_config[i/filtered_paramcount][1])+" ");
                        for j in range(0, dim_x/3):
                                f_out.write(". "+phase_name[j % 2]+" "+join_sel[(j/2) % len(join_sel)]+" ");
                        f_out.write("\n");

                        f_out.write(". ");
                        for j in range(0, dim_x):
                                f_out.write(column_name[j % 3]+" ");
                        f_out.write("\n");

                f_out.write(param_name[i % filtered_paramcount]+" ");
                for j in range(0, dim_x):
                        sprs[i][j] = sprs[i][j]/scale;
                        f_out.write(str(int(sprs[i][j]))+" ");
                f_out.write("\n");

        f_out.close();


def writeresult_inteljoin(result_filename, sprs, scale, dim_x, dim_y, tc):
        print("Writing "+result_filename);
        f_out = open(result_filename, 'w');

        for i in range(0, dim_y):
                for j in range(0, dim_x):
                        sprs[i][j] = sprs[i][j]/scale;
                        f_out.write(str(int(sprs[i][j]))+" ");
                f_out.write("\n");

        f_out.close();


def writeresult_stresstest(result_filename, sprs, scale, dim_x, dim_y, top, tc, m, sr):
        print("Writing "+result_filename);
        f_out = open(result_filename, 'w');

        for i in range(0, dim_y):
                if (i % filtered_paramcount == 0):
                        f_out.write("\n\n");
                        f_out.write(top+"/tc"+tc+"/m"+str(m[i/filtered_paramcount])+" ");
                        for j in range(0, dim_x/3):
                                f_out.write(" . sr"+str(sr[j])+" . ");
                        f_out.write("\n");

                        f_out.write(". ");
                        for j in range(0, dim_x):
                                f_out.write(column_name[j % 3]+" ");
                        f_out.write("\n");

                f_out.write(param_name[i % filtered_paramcount]+" ");

                for j in range(0, dim_x):
                        sprs[i][j] = sprs[i][j]/scale;
                        f_out.write(str(int(sprs[i][j]))+" ");
                f_out.write("\n");

        f_out.close();


def writeresult_repairest(result_filename, sprs, scale, dim_x, dim_y, top, tc, m, f):
        print("Writing "+result_filename);
        f_out = open(result_filename, 'w');

        for i in range(0, dim_y):
                if (i % filtered_paramcount == 0):
                        f_out.write("\n\n");
                        f_out.write(top+"/tc"+tc+"/m"+str(m[i/filtered_paramcount])+" ");
                        for j in range(0, dim_x/3):
                                f_out.write(" . f"+str(f[j])+" . ");
                        f_out.write("\n");

                        f_out.write(". ");
                        for j in range(0, dim_x):
                                f_out.write(column_name[j % 3]+" ");
                        f_out.write("\n");

                f_out.write(param_name[i % filtered_paramcount]+" ");

                for j in range(0, dim_x):
                        sprs[i][j] = sprs[i][j]/scale;
                        f_out.write(str(int(sprs[i][j]))+" ");
                f_out.write("\n");

        f_out.close();


def writeresult_repairsampling(result_filename, sprs, scale, dim_x, dim_y, top, tc, js, ft):
        print("Writing "+result_filename);
        f_out = open(result_filename, 'w');

        for i in range(0, dim_y):
                if (i % filtered_paramcount == 0):
                        f_out.write("\n\n");
                        f_out.write(top+"/tc"+tc+"/js"+str(js[i/filtered_paramcount])+" ");
                        for j in range(0, dim_x/3):
                                f_out.write(" . f"+str(ft[j])+" . ");
                        f_out.write("\n");

                        f_out.write(". ");
                        for j in range(0, dim_x):
                                f_out.write(column_name[j % 3]+" ");
                        f_out.write("\n");

                f_out.write(param_name[i % filtered_paramcount]+" ");

                for j in range(0, dim_x):
                        sprs[i][j] = sprs[i][j]/scale;
                        f_out.write(str(int(sprs[i][j]))+" ");
                f_out.write("\n");

        f_out.close();


def add_spr(spr1, spr2, dim_x, dim_y):
	for i in range(0, dim_y):	
		for j in range(0, dim_x/3):
			spr1[i][3*j] = spr1[i][3*j]+spr2[i][3*j];
			spr1[i][3*j+1] = spr1[i][3*j+1]+spr2[i][3*j+1];
			spr1[i][3*j+2] = spr1[i][3*j+2]+spr2[i][3*j+2];
#			if (spr2[i][3*j+1] > spr1[i][3*j+1]):
#				spr1[i][3*j+1] = spr2[i][3*j+1];
#				spr1[i][3*j+2] = spr2[i][3*j+2];

	return spr1;


def get_confint(spr1, spr2, N):
	return 1.96*Numeric.sqrt(abs(spr1-1.0*spr2*spr2/N)/(N-1))/math.sqrt(N);


def join_methods():
#        test_topologies = ('100-n-3-1', '100-n-3-2', '100-n-3-3');
	test_topologies = ('100-n-3-1',);
        test_tc = ('1', '2', '3', '4', '5', '6', '7', '8', '9');
 	srcsel_config = (('10','1'), ('6','2'), ('2','2'), ('2','6'), ('1','10'));
#        join_sel = ('5', '10', '20');
	join_sel = ('10', '20');
#        test_methods = (0, 1, 2, 3, 4, 5, 6);
	test_methods = (8,);
        window_size = ('1', '3');
#	query_type = ('2', '5', '6');
	query_type = ('2', '5');

	dim_y = filtered_paramcount*len(srcsel_config);
	dim_x = 3*2*len(test_methods)*len(join_sel);
	spreadsheet = Numeric.zeros((dim_y, dim_x), 'l');
	
	print("Spread sheet dimensions: "+str(dim_y)+"-"+str(dim_x));

	for ws in window_size:
		for qt in query_type:	
			sprs_avg = Numeric.zeros((dim_y, dim_x), 'l');
			sprs_var = Numeric.zeros((dim_y, dim_x), 'l');
			for top in test_topologies:
				sprs_topavg = Numeric.zeros((dim_y, dim_x), 'l');
				sprs_topvar = Numeric.zeros((dim_y, dim_x), 'l');
				for tc in test_tc:
					ofs_x = 0; 
					ofs_y = 0;
					for ssel in srcsel_config:
						for m in test_methods:
							for js in join_sel:
								common_dirname = top+"/tc"+tc+"/qt"+qt+"/js"+str(js)+"/ss"+str(ssel[0])+"-"+str(ssel[1])+"/ws"+ws+"/method"+str(m)+"/";

								s = gen_stats("exp"+exp_id+"/joinroute_est-"+common_dirname);
								for i in range(0, filtered_paramcount):
									for j in range(0, 3):
										spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];																

								ofs_x = ofs_x+3;
											
                                                	        s = gen_stats("exp"+exp_id+"/joinroute_sampling-"+common_dirname);
                                                        	for i in range(0, filtered_paramcount):
                                                                	for j in range(0, 3):
                                                                        	spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];
								ofs_x = ofs_x+3;
	
						ofs_y = ofs_y+filtered_paramcount;
						ofs_x = 0;
				

					writeresult_joinmethods("exp"+exp_id+"/analysis/"+method+"-"+top+"-tc"+tc+"-qt"+qt+"-ws"+ws+".txt", spreadsheet, 1, dim_x, dim_y, top, tc, srcsel_config, join_sel);

					sprs_topavg = add_spr(sprs_topavg, spreadsheet, dim_x, dim_y);
					sprs_avg = add_spr(sprs_avg, spreadsheet, dim_x, dim_y);					
                	                sprs_topvar = sprs_topvar+1.0*spreadsheet*spreadsheet;
                        	        sprs_var = sprs_var+1.0*spreadsheet*spreadsheet;

		                writeresult_joinmethods("exp"+exp_id+"/analysis/"+method+"-"+top+"-qt"+qt+"-ws"+ws+"-var.txt", get_confint(sprs_topvar, sprs_topavg, len(test_tc)), 1, dim_x, dim_y, top, "all", srcsel_config, join_sel);
		                writeresult_joinmethods("exp"+exp_id+"/analysis/"+method+"-"+top+"-qt"+qt+"-ws"+ws+".txt", sprs_topavg, len(test_tc), dim_x, dim_y, top, "all", srcsel_config, join_sel);

        		writeresult_joinmethods("exp"+exp_id+"/analysis/"+method+"-qt"+qt+"-ws"+ws+"-var.txt", get_confint(sprs_var, sprs_avg, len(test_tc)*len(test_topologies)), 1, dim_x, dim_y, "all", "all", srcsel_config, join_sel);
		        writeresult_joinmethods("exp"+exp_id+"/analysis/"+method+"-qt"+qt+"-ws"+ws+".txt", sprs_avg, len(test_tc)*len(test_topologies), dim_x, dim_y, "all", "all", srcsel_config, join_sel);


def join_methods_learn():
        test_topologies = ('100-n-3-1',);
        test_tc = ('1', '2', '3', '4', '5', '6', '7', '8', '9');
        srcsel_1 = ('10','1');
        srcsel_2 = ('1','10');
        jsel_1 = '20';
        jsel_2 = '5';
        param_config = (
          ((srcsel_1, srcsel_1, srcsel_2), (jsel_1, jsel_1, jsel_2), ('0', '0'), '0'),
          ((srcsel_2, srcsel_1, srcsel_2), (jsel_2, jsel_1, jsel_2), ('0', '0'), '0'),
          ((srcsel_1, srcsel_1, srcsel_2), (jsel_1, jsel_1, jsel_2), ('0', '0'), '1'),
          ((srcsel_2, srcsel_1, srcsel_2), (jsel_2, jsel_1, jsel_2), ('0', '0'), '1'),
          ((srcsel_1, srcsel_1, srcsel_2), (jsel_1, jsel_1, jsel_2), ('0', '1'), '0'),
          ((srcsel_2, srcsel_1, srcsel_2), (jsel_2, jsel_1, jsel_2), ('0', '1'), '0'),
          ((srcsel_1, srcsel_1, srcsel_2), (jsel_1, jsel_1, jsel_2), ('1', '1'), '0'),
          ((srcsel_1, srcsel_1, srcsel_2), (jsel_1, jsel_1, jsel_2), ('0', '1'), '1'),
          ((srcsel_2, srcsel_1, srcsel_2), (jsel_2, jsel_1, jsel_2), ('0', '1'), '1')
        );
	window_size = ('1',);
        sampling_cycles = (100,);
        sampling_time = (80000,);
	query_type = ('5',);

	dim_y = filtered_paramcount;
	dim_x = 3*len(param_config);
	spreadsheet = Numeric.zeros((dim_y, dim_x), 'l');
	
	print("Spread sheet dimensions: "+str(dim_y)+"-"+str(dim_x));

	for ws in window_size:
		for qt in query_type:	
			sprs_avg = Numeric.zeros((dim_y, dim_x), 'l');
			sprs_var = Numeric.zeros((dim_y, dim_x), 'l');
			for top in test_topologies:
				sprs_topavg = Numeric.zeros((dim_y, dim_x), 'l');
				sprs_topvar = Numeric.zeros((dim_y, dim_x), 'l');
				for tc in test_tc:
					ofs_x = 0; 
					ofs_y = 0;
                                        for pc in param_config:
                                                srcsel_config = pc[0];
                                                join_sel = pc[1];
                                                groupsel = pc[2];
                                                learn = pc[3];

                                                common_dirname = top+'/tc'+tc+'/qt'+qt+'/js'+join_sel[0]+"-"+join_sel[1]+"-"+join_sel[2]+'/ss'+srcsel_config[0][0]+"-"+srcsel_config[0][1]+"-"+srcsel_config[1][0]+"-"+srcsel_config[1][1]+"-"+srcsel_config[2][0]+"-"+srcsel_config[2][1]+'/ws'+ws+'/est'+learn+'/gs'+groupsel[0]+"-"+groupsel[1]+"/";
                                                s = gen_stats("exp"+exp_id+"/joinroute_sampling-"+common_dirname);
                                                for i in range(0, filtered_paramcount):
                                                        for j in range(0, 3):
                                                                spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];
                                                ofs_x = ofs_x+3;
										

					writeresult_joinmethods_learn("exp"+exp_id+"/analysis/"+method+"-"+top+"-tc"+tc+"-qt"+qt+"-ws"+ws+".txt", spreadsheet, 1, dim_x, dim_y, top, tc);

					sprs_topavg = add_spr(sprs_topavg, spreadsheet, dim_x, dim_y);
					sprs_avg = add_spr(sprs_avg, spreadsheet, dim_x, dim_y);
                	                sprs_topvar = sprs_topvar+1.0*spreadsheet*spreadsheet;
                        	        sprs_var = sprs_var+1.0*spreadsheet*spreadsheet;

		                writeresult_joinmethods_learn("exp"+exp_id+"/analysis/"+method+"-"+top+"-qt"+qt+"-ws"+ws+"-var.txt", get_confint(sprs_topvar, sprs_topavg, len(test_tc)), 1, dim_x, dim_y, top, "all");
		                writeresult_joinmethods_learn("exp"+exp_id+"/analysis/"+method+"-"+top+"-qt"+qt+"-ws"+ws+".txt", sprs_topavg, len(test_tc), dim_x, dim_y, top, "all");

        		writeresult_joinmethods_learn("exp"+exp_id+"/analysis/"+method+"-qt"+qt+"-ws"+ws+"-var.txt", get_confint(sprs_var, sprs_avg, len(test_tc)*len(test_topologies)), 1, dim_x, dim_y, "all", "all");
		        writeresult_joinmethods_learn("exp"+exp_id+"/analysis/"+method+"-qt"+qt+"-ws"+ws+".txt", sprs_avg, len(test_tc)*len(test_topologies), dim_x, dim_y, "all", "all");


def join_var():
#        test_topologies = ('100-n-3-1', '100-n-3-2', '100-n-3-3');
        test_topologies = ('100-n-3-1',);
        test_tc = ('1', '2', '3', '4', '5', '6', '7', '8', '9');
#        jpsel_config = (('10','1'), ('6','2'), ('4','2'), ('3','2'), ('2','2'), ('2','3'), ('2','4'), ('2','6'), ('1','10'));
	jpsel_config = (('10','1'), ('6','2'), ('2','2'), ('2','6'), ('1','10'));
        realsel_config = jpsel_config;
#	join_sel = ('5', '10', '20');
	join_sel = ('5',);
#        test_method = ('5', '6');
	test_method = ('5',);
#        window_size = ('1', '3');
	window_size = ('3',);
#	query_type = ('0', '2', '5', '6');
	query_type = ('0',);

	dim_y = filtered_paramcount*len(realsel_config);
	dim_x = 3*len(jpsel_config);
	spreadsheet = Numeric.zeros((dim_y, dim_x), 'l');
	
	print("Spread sheet dimensions: "+str(dim_y)+"-"+str(dim_x));

	for ws in window_size:
		for qt in query_type:
			for js in join_sel:
				for m in test_method:
					sprs_avg = Numeric.zeros((dim_y, dim_x), 'l');
					sprs_var = Numeric.zeros((dim_y, dim_x), 'l');
					for top in test_topologies:
						sprs_topavg = Numeric.zeros((dim_y, dim_x), 'l');
						sprs_topvar = Numeric.zeros((dim_y, dim_x), 'l');
						for tc in test_tc:
							ofs_x = 0; 
							ofs_y = 0;
							for rs in realsel_config:
								for jp in jpsel_config:
									common_dirname = top+"/tc"+tc+"/qt"+qt+"/js"+str(js)+"/ss_real"+str(rs[0])+"-"+str(rs[1])+"/ss_used"+str(jp[0])+"-"+str(jp[1])+"/ws"+ws+"/method"+m+"/";

									s = gen_stats("exp"+exp_id+"/joinroute_sampling-var-"+common_dirname);
									for i in range(0, filtered_paramcount):
										for j in range(0, 3):
											spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];																

									ofs_x = ofs_x+3;

								ofs_y = ofs_y+filtered_paramcount;
								ofs_x = 0;
				
															
	                                              	writeresult_joinvar("exp"+exp_id+"/analysis/"+method+"-"+top + "-tc"+tc+"-qt"+qt+"-ws"+ws+"-js"+js+"-m"+m+".txt", spreadsheet, 1, dim_x, dim_y, top, tc, realsel_config, jpsel_config, join_sel);

							sprs_topavg = add_spr(sprs_topavg, spreadsheet, dim_x, dim_y);
							sprs_avg = add_spr(sprs_avg, spreadsheet, dim_x, dim_y);					
                	                		sprs_topvar = sprs_topvar+1.0*spreadsheet*spreadsheet;
		        	                        sprs_var = sprs_var+1.0*spreadsheet*spreadsheet;

						writeresult_joinvar("exp"+exp_id+"/analysis/"+method+"-"+top+"-qt"+qt + "-ws"+ws+"-js"+js+"-m"+m+"-var.txt", get_confint(sprs_topvar, sprs_topavg, len(test_tc)), 1, dim_x, dim_y, top, "all", realsel_config, jpsel_config, join_sel);			    
						writeresult_joinvar("exp"+exp_id+"/analysis/"+method+"-"+top+"-qt"+qt + "-ws"+ws+"-js"+js+"-m"+m+".txt", sprs_topavg, len(test_tc), dim_x, dim_y, top, "all", realsel_config, jpsel_config, join_sel);

		        		writeresult_joinvar("exp"+exp_id+"/analysis/"+method+"-qt"+qt+"-ws"+ws+"-js"+js+"-m"+m+"-var.txt", get_confint(sprs_var, sprs_avg, len(test_tc)*len(test_topologies)), 1, dim_x, dim_y, "all", "all", realsel_config, jpsel_config, join_sel);
				        writeresult_joinvar("exp"+exp_id+"/analysis/"+method+"-qt"+qt+"-ws"+ws+"-js"+js+"-m"+m+".txt", sprs_avg, len(test_tc)*len(test_topologies), dim_x, dim_y, "all", "all", realsel_config, jpsel_config, join_sel);


def ght_join():
        test_topologies = ('100-n-3-1', '100-n-3-2', '100-n-3-3');
        test_tc = ('1', '2', '3', '4', '5', '6', '7', '8', '9');
 	srcsel_config = (('10','1'), ('6','2'), ('2','2'), ('2','6'), ('1','10'));
        join_sel = ('5', '10', '20');
        window_size = ('1', '3');
	query_type = ('2', '5');

	dim_y = filtered_paramcount*len(srcsel_config);
	dim_x = 3*2*len(join_sel);
	spreadsheet = Numeric.zeros((dim_y, dim_x), 'l');
	
	print("Spread sheet dimensions: "+str(dim_y)+"-"+str(dim_x));

	for ws in window_size:
		for qt in query_type:	
			sprs_avg = Numeric.zeros((dim_y, dim_x), 'l');
			sprs_var = Numeric.zeros((dim_y, dim_x), 'l');
			for top in test_topologies:
				sprs_topavg = Numeric.zeros((dim_y, dim_x), 'l');
				sprs_topvar = Numeric.zeros((dim_y, dim_x), 'l');
				for tc in test_tc:
					ofs_x = 0; 
					ofs_y = 0;
					for ssel in srcsel_config:
                                                for js in join_sel:
                                                        common_dirname = top+"/tc"+tc+"/qt"+qt+"/js"+str(js)+"/ss"+str(ssel[0])+"-"+str(ssel[1])+"/ws"+ws+"/";

                                                        s = gen_stats("exp"+exp_id+"/joinroute_est-var-"+common_dirname);
                                                        for i in range(0, filtered_paramcount):
                                                                for j in range(0, 3):
                                                                        spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];

                                                        ofs_x = ofs_x+3;

                                                        s = gen_stats("exp"+exp_id+"/joinroute_sampling-var-"+common_dirname);
                                                        for i in range(0, filtered_paramcount):
                                                                for j in range(0, 3):
                                                                        spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];
                                                        ofs_x = ofs_x+3;
	
						ofs_y = ofs_y+filtered_paramcount;
						ofs_x = 0;

					writeresult_ghtjoin("exp"+exp_id+"/analysis/"+method+"-"+top+"-tc"+tc+"-qt"+qt+"-ws"+ws+".txt", spreadsheet, 1, dim_x, dim_y, top, tc, srcsel_config, join_sel);

					sprs_topavg = add_spr(sprs_topavg, spreadsheet, dim_x, dim_y);
					sprs_avg = add_spr(sprs_avg, spreadsheet, dim_x, dim_y);
                	                sprs_topvar = sprs_topvar+1.0*spreadsheet*spreadsheet;
                        	        sprs_var = sprs_var+1.0*spreadsheet*spreadsheet;

		                writeresult_ghtjoin("exp"+exp_id+"/analysis/"+method+"-"+top+"-qt"+qt+"-ws"+ws+"-var.txt", get_confint(sprs_topvar, sprs_topavg, len(test_tc)), 1, dim_x, dim_y, top, "all", srcsel_config, join_sel);
		                writeresult_ghtjoin("exp"+exp_id+"/analysis/"+method+"-"+top+"-qt"+qt+"-ws"+ws+".txt", sprs_topavg, len(test_tc), dim_x, dim_y, top, "all", srcsel_config, join_sel);

        		writeresult_ghtjoin("exp"+exp_id+"/analysis/"+method+"-qt"+qt+"-ws"+ws+"-var.txt", get_confint(sprs_var, sprs_avg, len(test_tc)*len(test_topologies)), 1, dim_x, dim_y, "all", "all", srcsel_config, join_sel);
		        writeresult_ghtjoin("exp"+exp_id+"/analysis/"+method+"-qt"+qt+"-ws"+ws+".txt", sprs_avg, len(test_tc)*len(test_topologies), dim_x, dim_y, "all", "all", srcsel_config, join_sel);


def intel_join():
        test_tc = ('1', '2', '3', '4', '5', '6', '7', '8', '9');
#	test_methods = ('0', '1', '2', '3');
	test_methods = ('1',);

	dim_y = filtered_paramcount;
	dim_x = 3*2;
	spreadsheet = Numeric.zeros((dim_y, dim_x), 'f');
	
	print("Spread sheet dimensions: "+str(dim_y)+"-"+str(dim_x));

        for m in test_methods:
        	sprs_topavg = Numeric.zeros((dim_y, dim_x), 'f');
                sprs_topvar = Numeric.zeros((dim_y, dim_x), 'l');
                for tc in test_tc:
                	ofs_x = 0;
                        ofs_y = 0;
                        common_dirname = "/tc"+tc+"/method"+m+"/";

                        s = gen_stats("exp"+exp_id+"/joinroute-intel-q3-est"+common_dirname);
                        for i in range(0, filtered_paramcount):
                        	for j in range(0, 3):
                                	spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];

                        ofs_x = ofs_x+3;

                        s = gen_stats("exp"+exp_id+"/joinroute-intel-q3-sampling"+common_dirname);
                        for i in range(0, filtered_paramcount):
                        	for j in range(0, 3):
                                	spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];
                        ofs_x = ofs_x+3;

                        writeresult_inteljoin("exp"+exp_id+"/analysis/"+method+"-tc"+tc+"-m"+m+".txt", spreadsheet, 1, dim_x, dim_y, tc);
                        sprs_topavg = add_spr(sprs_topavg, spreadsheet, dim_x, dim_y);
                        sprs_topvar = sprs_topvar+1.0*spreadsheet*spreadsheet;

                writeresult_inteljoin("exp"+exp_id+"/analysis/"+method+"-m"+m+"-var.txt", get_confint(sprs_topvar, sprs_topavg, len(test_tc)), 1, dim_x, dim_y, "all");
                writeresult_inteljoin("exp"+exp_id+"/analysis/"+method+"-m"+m+".txt", sprs_topavg, len(test_tc), dim_x, dim_y, "all");


def stress_test():
        test_topologies = ('100-n-3-1', '100-n-3-2', '100-n-3-3');
        top_dim = '60 60';
        test_tc = ('1', '2', '3', '4', '5', '6', '7', '8', '9');
        srcsel_config = ('2','6');
        join_sel = '20';
        test_methods = (0, 1, 5, 7);
        window_size = '3';
        sampling_cycles = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20, 25, 30, 40, 50, 60, 75, 100, 125, 150, 200, 300);
        query_types = '2';

	dim_y = filtered_paramcount*len(test_methods);
	dim_x = 3*len(sampling_cycles);
	spreadsheet = Numeric.zeros((dim_y, dim_x), 'l');

	print("Spread sheet dimensions: "+str(dim_y)+"-"+str(dim_x));

        sprs_avg = Numeric.zeros((dim_y, dim_x), 'l');
        sprs_var = Numeric.zeros((dim_y, dim_x), 'l');
        for top in test_topologies:
		jr_needed = Numeric.zeros((100, 100), 'l');
		jr_produced = Numeric.zeros((100, 100), 'l');
		try:
			f_jr = open("/home/DBGROUP/svilen/utils/joinresults/joinresults-"+top+"-"+srcsel_config[0]+"-"+srcsel_config[1]+"-"+join_sel+".txt", "r");	
                        f_pr = open("/home/DBGROUP/svilen/utils/joinresults/joinresults-p-"+top+"-"+srcsel_config[0]+"-"+srcsel_config[1]+"-"+join_sel+".txt", "r");
		except:
			print("Cannot open join result files");
			sys.exit();
	
		ll = f_jr.readlines();
		node_id = 0;
		for l in ll:
			l = l.split();
			for sample_id in range(0, 100):
				jr_needed[node_id][sample_id] = int(l[sample_id]);
			node_id = node_id+1;
				
		f_jr.close();

                ll = f_pr.readlines();
                node_id = 0;
                for l in ll:
                        l = l.split();
                        for sample_id in range(0, 100):
                                jr_produced[node_id][sample_id] = int(l[sample_id]);
                        node_id = node_id+1;

                f_pr.close();


        	sprs_topavg = Numeric.zeros((dim_y, dim_x), 'l');
                sprs_topvar = Numeric.zeros((dim_y, dim_x), 'l');
                for tc in test_tc:
                	ofs_x = 0;
                        ofs_y = 0;
                        for m in test_methods:
                                for sc in sampling_cycles:
                                        common_dirname = top+"/tc"+tc+"/qt"+str(query_types)+"/sr"+str(sc)+"/js"+str(join_sel)+"/ss"+str(srcsel_config[0])+"-"+str(srcsel_config[1])+"/method"+str(m)+"/ws"+str(window_size)+"/";

                                        s = gen_stats("exp"+exp_id+"/joinroute_sampling-"+common_dirname);
                                        for i in range(0, filtered_paramcount):
                                                for j in range(0, 3):
                                                        spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];


					try:
						f_log = open("exp"+exp_id+"/joinroute_sampling-"+common_dirname+"join_inlog0.txt", "r");
					except:
						print("Cannot open join inlog file for "+common_dirname);				
						sys.exit();

					jr_rcv = Numeric.zeros((100, 100), 'l');
					jr_rcvtime = Numeric.zeros((100, 100), 'l');

					ll = f_log.readlines();
					for i in range(0, len(ll)/2):
						l1 = ll[2*i].split();
						l2 = ll[2*i+1].split();
						
						rcv_time = int(l1[0]);
						for j in range(0, len(l2)/2):
							node_id = int(l2[2*j]);
							sample_id = int(l2[2*j+1])-1;
							if ((sample_id >= 0) & (sample_id < 100)):
								jr_rcv[node_id][sample_id] = 1;
								if (jr_rcvtime[node_id][sample_id] == 0) | (jr_rcvtime[node_id][sample_id] > rcv_time):
									jr_rcvtime[node_id][sample_id] = rcv_time;
						
					f_log.close();


					pairs_total = 0; pairs_rcv = 0; delay_total = 0;
					try:	
						f_pair = open("/home/DBGROUP/svilen/utils/joinresults/joinresults-pairs-"+top+"-"+srcsel_config[0]+"-"+srcsel_config[1]+"-"+join_sel+".txt", "r");
					except:
						print("Cannot open join pairs file!");
						sys.exit();
					
					ll = f_pair.readlines();
					for i in range(0, len(ll)):
						l = ll[i].split();
						node1 = int(l[0]); sample1 = int(l[1])-1; 
						node2 = int(l[2]); sample2 = int(l[3])-1;

						pairs_total = pairs_total+1;
						if (jr_rcv[node1][sample1] == 1) & (jr_rcv[node2][sample2] == 1):
							pairs_rcv = pairs_rcv+1;
							delay_total = delay_total + max(jr_rcvtime[node1][sample1], jr_rcvtime[node2][sample2])-max(sc*int(jr_produced[node1][sample1])+5, sc*int(jr_produced[node2][sample2])+5);
					
					f_pair.close();

					spreadsheet[ofs_y+filtered_paramcount-3][ofs_x] = 100*pairs_rcv/pairs_total;
					if (pairs_rcv > 0):
						spreadsheet[ofs_y+filtered_paramcount-2][ofs_x] = delay_total/pairs_rcv;
					else:
						spreadsheet[ofs_y+filtered_paramcount-2][ofs_x] = 0;
					spreadsheet[ofs_y+filtered_paramcount-1][ofs_x] = 0;

                                        ofs_x = ofs_x+3;

                                ofs_y = ofs_y+filtered_paramcount;
                                ofs_x = 0;

                        
			writeresult_stresstest("exp"+exp_id+"/analysis/"+method+"-"+top+"-tc"+tc+"-" + srcsel_config[0]+"-"+srcsel_config[1]+"-"+join_sel+".txt", spreadsheet, 1, dim_x, dim_y, top, tc, test_methods, sampling_cycles);

                        sprs_topavg = add_spr(sprs_topavg, spreadsheet, dim_x, dim_y);
                        sprs_avg = add_spr(sprs_avg, spreadsheet, dim_x, dim_y);
                        sprs_topvar = sprs_topvar+1.0*spreadsheet*spreadsheet;
                        sprs_var = sprs_var+1.0*spreadsheet*spreadsheet;

                writeresult_stresstest("exp"+exp_id+"/analysis/"+method+"-"+top + "-"+srcsel_config[0]+"-"+srcsel_config[1]+"-"+join_sel+"-var.txt", get_confint(sprs_topvar, sprs_topavg, len(test_tc)), 1, dim_x, dim_y, top, "all", test_methods, sampling_cycles);
                writeresult_stresstest("exp"+exp_id+"/analysis/"+method+"-"+top + "-"+srcsel_config[0]+"-"+srcsel_config[1]+"-"+join_sel+".txt", sprs_topavg, len(test_tc), dim_x, dim_y, top, "all", test_methods, sampling_cycles);

        writeresult_stresstest("exp"+exp_id+"/analysis/"+method + "-"+srcsel_config[0]+"-"+srcsel_config[1]+"-"+join_sel+"-var.txt", get_confint(sprs_var, sprs_avg, len(test_tc)*len(test_topologies)), 1, dim_x, dim_y, "all", "all", test_methods, sampling_cycles);
        writeresult_stresstest("exp"+exp_id+"/analysis/"+method + "-"+srcsel_config[0]+"-"+srcsel_config[1]+"-"+join_sel+".txt", sprs_avg, len(test_tc)*len(test_topologies), dim_x, dim_y, "all", "all", test_methods, sampling_cycles);


def stress_testintel():
        test_tc = ('1', '2', '3', '4', '5', '6', '7', '8', '9');
        test_methods = (0, 1, 2, 3);
#	test_methods = (3,);
        sampling_cycles = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20, 25, 30, 40, 50, 60, 75, 100, 125, 150, 200, 300);

	dim_y = filtered_paramcount*len(test_methods);
	dim_x = 3*len(sampling_cycles);
	spreadsheet = Numeric.zeros((dim_y, dim_x), 'l');

	print("Spread sheet dimensions: "+str(dim_y)+"-"+str(dim_x));

        jr_produced = Numeric.zeros((55, 656), 'l');
        try:
                f_pr = open("/home/DBGROUP/svilen/utils/joinresults/results_intel.txt", "r");
        except:
                print("Cannot open join result file");
                sys.exit();

        ll = f_pr.readlines();
        node_id = 0;
        for l in ll:
                l = l.split();
                for sample_id in range(0, 656):
                        jr_produced[node_id][sample_id] = int(l[sample_id]);
                node_id = node_id+1;

        f_pr.close();

	sprs_topavg = Numeric.zeros((dim_y, dim_x), 'l');
        sprs_topvar = Numeric.zeros((dim_y, dim_x), 'l');

	for tc in test_tc:
                ofs_x = 0;
               	ofs_y = 0;
		
		for m in test_methods:
                        for sc in sampling_cycles:
                                common_dirname = "/tc"+tc+"/sr"+str(sc)+"/method"+str(m)+"/";

                                s = gen_stats("exp"+exp_id+"/joinroute-intel-q3-sampling"+common_dirname);
                                for i in range(0, filtered_paramcount):
                                        for j in range(0, 3):
                                                spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];

                                try:
                                        f_log = open("exp"+exp_id+"/joinroute-intel-q3-sampling"+common_dirname+"join_inlog0.txt", "r");
                                except:
                                        print("Cannot open join inlog file for "+common_dirname);
                                        sys.exit();

                                rcv_count = 0; rcv_delay = 0;
                                ll = f_log.readlines();
                                for i in range(0, len(ll)/2):
                                        l1 = ll[2*i].split();
                                    	l2 = ll[2*i+1].split();

				    	first_nodeid = int(l2[0]);
                                    	first_sampleid = int(l2[1])-1;

                                    	rcv_time = int(l1[0]);
                                    	for j in range(0, len(l2)/2):
                                                node_id = int(l2[2*j]);
                                            	sample_id = int(l2[2*j+1])-1;
                                            	if (sample_id >= 0):
                                                        rcv_count = rcv_count+1;
							d = rcv_time-sc*jr_produced[first_nodeid][first_sampleid]-5;
                                                        rcv_delay = rcv_delay+d;

							if (sc == 300) & ((d > 100) | (d < -5)) & (m == 0):
								print("Problem for "+str(node_id)+","+str(sample_id)+", rcv: "+str(rcv_time)+", prod:"+str(jr_produced[first_nodeid][first_sampleid]));
								sys.exit();


                                f_log.close();

                                try:
                                        f_drop = open("exp"+exp_id+"/joinroute-intel-q3-sampling"+common_dirname+"cmd_drop.log", "r");
                                        ll = f_drop.readlines();
                                        msg_drop = len(ll);
                                        f_drop.close();
                                except:
                                        msg_drop = 0;
                                        
                                spreadsheet[ofs_y+filtered_paramcount-2][ofs_x] = rcv_delay/rcv_count;
                                spreadsheet[ofs_y+filtered_paramcount-1][ofs_x] = msg_drop;

                      
                                ofs_x = ofs_x+3;

                        ofs_y = ofs_y+filtered_paramcount;
                        ofs_x = 0;

                writeresult_stresstest("exp"+exp_id+"/analysis/"+method+"-tc"+tc+".txt", spreadsheet, 1, dim_x, dim_y, "intel", tc, test_methods, sampling_cycles);

	        sprs_topavg = add_spr(sprs_topavg, spreadsheet, dim_x, dim_y);
        	sprs_topvar = sprs_topvar+1.0*spreadsheet*spreadsheet;

	writeresult_stresstest("exp"+exp_id+"/analysis/"+method+"-var.txt", get_confint(sprs_topvar, sprs_topavg, len(test_tc)), 1, dim_x, dim_y, "intel", "all", test_methods, sampling_cycles);
        writeresult_stresstest("exp"+exp_id+"/analysis/"+method+".txt", sprs_topavg, len(test_tc), dim_x, dim_y, "intel", "all", test_methods, sampling_cycles);

        
def repair_est():
        test_topologies = ('100-n-3-1', '100-n-3-2', '100-n-3-3');
        test_tc = ('1', '2', '3', '4', '5', '6', '7', '8', '9');
        srcsel_config = (('2','2'),);
        join_sel = ('20',);
        window_size = ('3',);
        test_methods = ('2', '5');
	fail_nodes = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15');
	rl = '3';

        dim_y = filtered_paramcount;
        dim_x = 3*len(fail_nodes);
        spreadsheet = Numeric.zeros((dim_y, dim_x), 'l');

        print("Spread sheet dimensions: "+str(dim_y)+"-"+str(dim_x));

	for ws in window_size:
		for m in test_methods:
                        sprs_avg = Numeric.zeros((dim_y, dim_x), 'l');
                        sprs_var = Numeric.zeros((dim_y, dim_x), 'l');
                        for top in test_topologies:
                                sprs_topavg = Numeric.zeros((dim_y, dim_x), 'l');
                                sprs_topvar = Numeric.zeros((dim_y, dim_x), 'l');
                                for tc in test_tc:
                                        ofs_x = 0;
                                        ofs_y = 0;
                                        for ssel in srcsel_config:
                                                for js in join_sel:
							for fn in fail_nodes:
	                                                        common_dirname = top+"/tc"+tc+"/f"+fn+"/js"+str(js)+"/ss"+str(ssel[0])+"-"+str(ssel[1])+"/method"+m+"/ws"+ws+"/rl"+str(rl)+"/";

	                                                        s = gen_stats("exp"+exp_id+"/joinroute_est-"+common_dirname);
        	                                                for i in range(0, filtered_paramcount):
                	                                                for j in range(0, 3):
                        	                                                spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];

								join_pair = Numeric.zeros((100, 100), 'l');
								join_node = Numeric.zeros((100, 100), 'l');
								join_node1 = Numeric.zeros((100, 100, 100), 'l');
								join_decision = Numeric.zeros((100, 1), 'l');
								for i in range(0, 100):
									try:
										f_jp = open("exp"+exp_id+"/joinroute_est-"+common_dirname+"join_nodes"+str(i)+".txt", "r");
									except:				
										continue;
										
									ll = f_jp.readlines();
									for k in range(0, len(ll)):
										l = ll[k].split();
										join_pair[i][int(l[2])] = 1;
										join_node[i][int(l[2])] = int(l[1]);
									f_jp.close();

									try:
										f_dec = open("exp"+exp_id+"/joinroute_est-"+common_dirname+"coord_decision"+str(i)+".txt", "r");
									except:
										continue;
										
									ll = f_dec.readlines();
									for k in range(0, len(ll)):
										l = ll[k].split();
										join_decision[i] = int(l[3]);
									f_dec.close();			


								for i in range(0, 100):	
									try:
										f_jp1 = open("exp"+exp_id+"/joinroute_est-"+common_dirname+"join_pairs"+str(i)+".txt", "r");
                                                                        except:
                                                                                continue;

                                                                        ll = f_jp1.readlines();
                                                                        for k in range(0, len(ll)):
                                                                                l = ll[k].split();
                                                                                join_node1[int(l[1])][int(l[2])][i] = 1;
                                                                                join_node1[int(l[2])][int(l[1])][i] = 1;
                                                                        f_jp1.close();

									
								pairs_correct = 0;
								for i in range(0, 25):
									for j in range(50, 100):
										if (join_pair[i][j] == 1) & (join_pair[j][i] == 1) & (join_node[i][j] == join_node[j][i]) & (join_node1[i][j][join_node[i][j]] == 1) & (join_decision[i] == join_decision[j]):
											pairs_correct = pairs_correct+1;

								if (fn == '0') & (pairs_correct == 0):
									print(join_pair);
									print("No correct pairs for no failed nodes!");
									sys.exit();

								spreadsheet[ofs_y+filtered_paramcount-1][ofs_x] = pairs_correct;
								

								ofs_x = ofs_x+3;

                                                ofs_y = ofs_y+filtered_paramcount;
                                                ofs_x = 0;

                                        writeresult_repairest("exp"+exp_id+"/analysis/"+method+"-"+top+"-tc"+tc+"-m"+m+"-ws"+ws+"-rl"+rl+".txt", spreadsheet, 1, dim_x, dim_y, top, tc, m, fail_nodes)

                                        sprs_topavg = add_spr(sprs_topavg, spreadsheet, dim_x, dim_y);
                                        sprs_avg = add_spr(sprs_avg, spreadsheet, dim_x, dim_y);
                                        sprs_topvar = sprs_topvar+1.0*spreadsheet*spreadsheet;
                                        sprs_var = sprs_var+1.0*spreadsheet*spreadsheet;

                                writeresult_repairest("exp"+exp_id+"/analysis/"+method+"-"+top+"-m"+m+"-ws"+ws+"-rl"+rl+"-var.txt", get_confint(sprs_topvar, sprs_topavg, len(test_tc)), 1, dim_x, dim_y, top, "all", m, fail_nodes);
                                writeresult_repairest("exp"+exp_id+"/analysis/"+method+"-"+top+"-m"+m+"-ws"+ws+"-rl"+rl+".txt", sprs_topavg, len(test_tc), dim_x, dim_y, top, "all", m, fail_nodes);

                        writeresult_repairest("exp"+exp_id+"/analysis/"+method+"-m"+m+"-ws"+ws+"-rl"+rl+"-var.txt", get_confint(sprs_var, sprs_avg, len(test_tc)*len(test_topologies)), 1, dim_x, dim_y, "all", "all", m, fail_nodes);
                        writeresult_repairest("exp"+exp_id+"/analysis/"+method+"-m"+m+"-ws"+ws+"-rl"+rl+".txt", sprs_avg, len(test_tc)*len(test_topologies), dim_x, dim_y, "all", "all", m, fail_nodes);


def repair_sampling():
	top = '100-n-3-1';
        test_tc = ('1', '2', '3', '4', '5', '6', '7', '8', '9');
        test_methods = ('5',);
        srcsel_config = (('2','2'),);
        join_sel = ('5', '10', '20');
        fail_times = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
        window_size = ('3',)
	rl = '2';

	dim_y = filtered_paramcount*len(join_sel);
	dim_x = 3*len(fail_times);
	spreadsheet = Numeric.zeros((dim_y, dim_x), 'l');

	print("Spread sheet dimensions: "+str(dim_y)+"-"+str(dim_x));

	sprs_topavg = Numeric.zeros((dim_y, dim_x), 'l');
        sprs_topvar = Numeric.zeros((dim_y, dim_x), 'l');
	for tc in test_tc:
                ofs_x = 0;
               	ofs_y = 0;
		for ws in window_size:
			for m in test_methods:
        	                for ss in srcsel_config:
                	                for js in join_sel:
                        	                for ft in fail_times:
                                	                common_dirname = top+"/tc"+tc+"/qt0"+"/f"+str(ft)+"/js"+str(js)+"/ss"+ss[0]+"-"+ss[1]+"/method"+str(m)+"/ws"+str(ws)+"/rl"+str(rl)+"/";

	                                                s = gen_stats("exp"+exp_id+"/joinroute_sampling-"+common_dirname);
        	                                        for i in range(0, filtered_paramcount):
                	                                        for j in range(0, 3):
                        	                                        spreadsheet[ofs_y+i][ofs_x+j] = s[i][j];

                                	                jr_produced = Numeric.zeros((100, 100), 'l');
							jr_delay = Numeric.zeros((100, 100), 'l');
                                        	        for i in range(0, 100):
                                                	        try:
                                                        	        f_sample = open("exp"+exp_id+"/joinroute_sampling-"+common_dirname+"sample_log"+str(i)+".txt", "r");
	                                                        except:
        	                                                        continue;

                	                                        ll = f_sample.readlines();
                        	                                for j in range(0,len(ll)):
                                	                                jr_produced[i][j] = int(ll[j].split()[0]);
                                        	                f_sample.close();


                                                	try:
                                                        	f_log = open("exp"+exp_id+"/joinroute_sampling-"+common_dirname+"join_inlog0.txt", "r");
	                                                except:
        	                                                print("Cannot open join log file!");
                	                                        sys.exit();

							rcv_count = 0; rcv_delay = 0;
                                	                ll = f_log.readlines();
                                        	        for i in range(0, len(ll)/2):
                                                	        l1 = ll[2*i].split();
                                                        	l2 = ll[2*i+1].split();

	                                                        first_nodeid = int(l2[0]);
        	                                                first_sampleid = int(l2[1]);
                	                                        rcv_time = int(l1[0]);
                        	                                for j in range(0, len(l2)/2):
                                	                                node_id = int(l2[2*j]);
                                        	                        sample_id = int(l2[2*j+1]);
                                                	                if (sample_id >= 0):                                         	                
                                                                	        d = rcv_time-jr_produced[first_nodeid][first_sampleid];
										if (jr_delay[node_id][sample_id] == 0) | (jr_delay[node_id][sample_id] > d):
											jr_delay[node_id][sample_id] = d;    
											rcv_count = rcv_count+1;
											rcv_delay = rcv_delay+d;	

	                                                f_log.close();

        	                                        spreadsheet[ofs_y+filtered_paramcount-1][ofs_x] = rcv_delay/rcv_count;

                	                                ofs_x = ofs_x+3;

                                        	ofs_y = ofs_y+filtered_paramcount;
	                                        ofs_x = 0;

                writeresult_repairsampling("exp"+exp_id+"/analysis/"+method+"-tc"+tc+".txt", spreadsheet, 1, dim_x, dim_y, "intel", tc, join_sel, fail_times);

	        sprs_topavg = add_spr(sprs_topavg, spreadsheet, dim_x, dim_y);
        	sprs_topvar = sprs_topvar+1.0*spreadsheet*spreadsheet;

	writeresult_repairsampling("exp"+exp_id+"/analysis/"+method+"-var.txt", get_confint(sprs_topvar, sprs_topavg, len(test_tc)), 1, dim_x, dim_y, "intel", "all", join_sel, fail_times);
        writeresult_repairsampling("exp"+exp_id+"/analysis/"+method+".txt", sprs_topavg, len(test_tc), dim_x, dim_y, "intel", "all", join_sel, fail_times);


#---------------------------------------------------MAIN------------------------------------------------
if (len(sys.argv) < 3):
	print("Invalid number of arguments");
	sys.exit();

method = sys.argv[1];
exp_id = sys.argv[2];
node_count = int(sys.argv[3]);

os.system("mkdir -p exp"+exp_id+"/analysis");
if (method == "joinmethods"):
	join_methods();
elif (method == "joinmethodslearn"):
	join_methods_learn();
elif (method == "joinvar"):
	join_var();
elif (method == "ghtjoin"):
        ght_join();
elif (method == "joinintel"):
        intel_join();
elif (method == "stresstest"):
        stress_test();
elif (method == "joinintelstress"):
        stress_testintel();
elif (method == "repairest"):
	repair_est();
elif (method == "repairsampling"):
	repair_sampling();
else :
	print("Invalid method");
	sys.exit();
